package com.baidu.bm.web;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.baidu.bm.util.Constants;

/**
 * 登录页面控制器.
 * 仅做演示，未来切换到UUAP
 * 
 * @author GuoLin
 *
 */
@Controller
@RequestMapping("/")
public class LoginController {

	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@RequestMapping
	public String index(ServletRequest request) {
		logger.debug("A IITer is visiting us: [{} ({})]", request.getRemoteHost(), request.getRemoteAddr());
		return "index";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(@RequestParam("username") String username, 
			@RequestParam("password") String password, 
			HttpSession session) {
		
		logger.info("[{}] want to login with password [{}]", username, password);
		
		ModelAndView mav = new ModelAndView();

		// 验证用户名密码正确性
		if ("guolin".equals(username) && "guolin".equals(password)) {
			session.setAttribute(Constants.SESSION_USER, username);
			mav.setViewName("redirect:books");
		} else {
			mav.setViewName("index");
		}
		
		return mav;
	}

	@RequestMapping("/logout")
	public ModelAndView logout() {
		ModelAndView mav = new ModelAndView();
		return mav;
	}

}
